﻿using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.DataCollection.Model;
using ZR.DataCollection.OperationCore.OperationEnum;
using ZR.DataCollection.OperationCore.OperationHelper;
using ZR.DataCollection.SingleParameter;
using ZR.Model.Models.ProductionControl;
using ZR.Service.IService.ProductionControl;

namespace ZR.DataCollection.OperationCore.OperationMode
{
    /// <summary>
    /// 数据保存请求处理
    /// </summary>
    public sealed class DataSave : OperationInfoBase
    {
        public override event Action<string> Event_Notification;

        private readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        private bool collectStatus = true;

        public DataSave(int operationType, string stationType) : base(operationType, stationType)
        {
        }

        public override void WorkDetail(ImpParameter imp)
        {
            DataDock? dataDock = imp.objectParam as DataDock;
            var keyValuePairs = CurrentCruxPoint(dataDock.LineCode, dataDock.ProductType, dataDock.ItemNo);
            SpotHandle(imp, keyValuePairs);
        }

        public override void SpotHandle(ImpParameter parameter, Dictionary<int, List<OperationPointConfig>> SpotGroup)
        {
            try
            {
                foreach (var SpotInfo in SpotGroup)
                {
                    switch (SpotInfo.Key)
                    {
                        case (int)DataOperateEnum.Read:
                            ReadFunction(parameter, SpotInfo.Value);
                            break;

                        case (int)DataOperateEnum.Write:
                            SpotInfo.Value.ForEach(x =>
                            {
                                if (!collectStatus && x.ItemSort.Equals(3))
                                {
                                    parameter.PLCDetail.WriteValue(x.ItemNo, x.ItemWriteValue);
                                    return;
                                }
                                parameter.PLCDetail.WriteValue(x.ItemNo, x.ItemWriteValue);
                            });
                            break;

                        case (int)DataOperateEnum.Verify:
                            break;

                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("数据保存Error", ex);
            }
        }

        private void ReadFunction(ImpParameter parameter, List<OperationPointConfig> operationPointList)
        {
            DataDock? dataDock = parameter.objectParam as DataDock;
            for (int i = 0; i < operationPointList.Count - 1; i = i + 3)
            {
            }
        }
    }
}